<template>
  <c-form-dialog
    ref="form"
    :visible.sync="innerVisible"
    :form-items="formItems"
    :form-data="formData"
    :table-name="tableName"
    :table-id="tableId"
    @close="handleClose"
    @success="handleSuccess"
    :before-save="handleBeforeSave"
  ></c-form-dialog>
</template>

<script>
import { enumRiskType } from '@/bigcredit/views/settle/risk/enum'
import { mul, div } from '@vimi/utils-tool'
export default {
  components: {},
  props: {
    visible: Boolean,
    formData: {
      type: Object,
      default() {
        return {}
      },
    },
  },
  data() {
    return {
      innerVisible: false,
      tableName: 'risk_funds',
      formItems: [
        {
          tag: 'select',
          itemAttrs: {
            label: '风险类型',
            rules: [{ required: true, message: '请选择风险类型', trigger: 'change' }],
          },
          attrs: {
            key: 'risk_type',
            value: '',
            options: enumRiskType,
          },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '风险类型描述',
          },
          attrs: {
            class: 'form-control-ml',
            key: 'risk_type_remark',
          },
          ifRender: model => +model.risk_type === 3,
        },

        {
          tag: 'input',
          itemAttrs: {
            label: '缴纳比例',
            rules: [{ required: true, message: '请输入缴纳比例', trigger: 'blur' }],
          },
          attrs: {
            key: 'pay_rate',
            placeholder: '请输入缴纳比例',
          },
          tagSlots: [{ name: 'append', slot: '%' }],
          directives: [
            {
              name: 'input-number',
              value: {
                min: 0,
                max: 100,
                fixed: 2,
              },
            },
          ],
          events: {
            input: model => {
              model.pay_funds = div(mul(this.formData.approved_funds, model.pay_rate), 100) || 0
            },
          },
        },

        {
          tag: 'input',
          itemAttrs: {
            label: '应缴金额',
            rules: [{ required: true, message: '请输入应缴金额', trigger: 'blur' }],
          },
          attrs: {
            key: 'pay_funds',
            placeholder: '请输入应缴金额',
          },
          tagSlots: [{ name: 'append', slot: '万元' }],
          directives: [
            {
              name: 'input-number',
              value: {
                min: 0,
                fixed: 4,
              },
            },
          ],
        },

        {
          tag: 'input',
          itemAttrs: {
            label: '实际缴纳金额',
            rules: [{ required: true, message: '请输入实际缴纳金额', trigger: 'blur' }],
          },
          attrs: {
            key: 'pay_funds_actual',
            placeholder: '请输入实际缴纳金额',
          },
          tagSlots: [{ name: 'append', slot: '万元' }],
          directives: [
            {
              name: 'input-number',
              value: {
                min: 0,
                fixed: 4,
              },
            },
          ],
        },

        {
          tag: 'textarea',
          itemAttrs: {
            label: '备注',
          },
          attrs: {
            class: 'form-control-ml',
            key: 'remark',
          },
        },
      ],
    }
  },
  computed: {
    tableId() {
      return this.formData.id || ''
    },
  },
  watch: {
    visible(val) {
      this.innerVisible = val
    },
  },
  created() {},
  mounted() {},
  methods: {
    handleBeforeSave(model) {
      return model
    },
    handleClose() {
      this.$emit('update:visible', false)
    },
    handleSuccess(res) {
      this.$emit('success', res)
    },
  },
}
</script>
<style lang="scss" scoped></style>
